/**
 * Created by gongxun on 16/11/20.
 */

var KwxTableResultLayer = GameCCSBase.extend({
    Image_results : null,
    Image_dealCard : null,
    Image_huangzhuang : null,
    AtlasLabel_scores : null,
    Image_socres : null,
    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/KwxTableResult.json");

        this.loadUI();
    },

    reset : function(){
        for (var i = 0; i < 3; i++){
            this.Image_results[i].setVisible(false);
            this.Image_results[i].stopAllActions();
        }

        this.Image_huangzhuang.setVisible(false);
        this.Image_huangzhuang.stopAllActions();
    },

    loadUI : function(){
        this.Image_results = [];
        for(var i = 0; i < 3; i++){
            var Image_result = mo.seekWidgetByName(this.ccsNode, "Image_result" + i);
            this.Image_results.push(Image_result);

            if (cc.sys.isNative){
                var opPartic = new cc.ParticleSystem("res/effect/game_op_light.plist");
                opPartic.setVisible(true);
                opPartic.setName("opPartic");
                Image_result.addChild(opPartic);
            }

            Image_result.setVisible(false);
        }

        this.Image_dealCard = [];
        for(var i = 0; i < 3; i++){
            var Image_dealCard = mo.seekWidgetByName(this.ccsNode, "Image_dealCard" + i);
            this.Image_dealCard.push(Image_dealCard);

            Image_dealCard.setVisible(false);

            var card = new KwxCard();
            Image_dealCard.addChild(card);
        }

        this.Image_huangzhuang = mo.seekWidgetByName(this.ccsNode, "Image_huangzhuang");
        this.Image_huangzhuang.setVisible(false);

        this.Image_socres = [];
        this.AtlasLabel_scores = [];
        for (var i = 0; i < 3; i++){
            var Image_socre = mo.seekWidgetByName(this.ccsNode, "Image_socre" + i);
            this.Image_socres.push(Image_socre);
            Image_socre.setVisible(false);

            var AtlasLabel_score = mo.seekWidgetByName(Image_socre, "AtlasLabel_score" + i);
            this.AtlasLabel_scores.push(AtlasLabel_score);

        }
    },

    //-1 不存在
    //0 亮
    //2 碰
    //4 明杠
    //5 暗杠
    //6 补杠
    //3 胡
    //33 自摸
    showResult : function(results){
        for (var i = 0; i < 3; i++){
            if (results[i] == -1){
                this.Image_results[i].setVisible(false);
                this.Image_results[i].stopAllActions();
            }else{
                var Image_result = this.Image_results[i];
                var Image_light = mo.seekWidgetByName(Image_result, "Image_light");
                var opPartic = mo.seekWidgetByName(Image_result, "opPartic");
                var Image_op = mo.seekWidgetByName(Image_result, "Image_op");

                mo.gameHelper.loadTextureImage(Image_result, this.getResultFile(results[i]));
                mo.gameHelper.loadTextureImage(Image_light, this.getResultLightFile(results[i]));
                mo.gameHelper.loadTextureImage(Image_op, this.getResultFile(results[i]));
                var centerPos = cc.p(Image_result.getContentSize().width / 2, Image_result.getContentSize().height / 2);
                Image_light.setPosition(centerPos);
                if (cc.sys.isNative) {
                    opPartic.setPosition(centerPos);
                }
                Image_op.setPosition(centerPos);

                if (mo.gameHelper.projectCode == 0){
                    //0 不要动画
                    Image_result.setVisible(true);
                    Image_result.runAction(cc.sequence(
                        cc.delayTime(0.5),
                        cc.hide()
                        )
                    );
                }else{
                    Image_result.setVisible(true);
                    Image_result.setScale(0.1);
                    Image_light.setOpacity(255);
                    Image_op.setVisible(false);
                    Image_op.setScale(1);
                    Image_op.setOpacity(255);

                    Image_result.stopAllActions();
                    Image_result.runAction(cc.sequence(
                        cc.scaleTo(0.1, 1.7),
                        cc.scaleTo(0.3, 1.4),
                        cc.scaleTo(0.05, 1.7),
                        cc.scaleTo(0.1, 1.3),
                        cc.callFunc(function () {
                            if (cc.sys.isNative) {
                                opPartic.start();
                            }
                        }),
                        cc.delayTime(0.5),
                        cc.hide()
                    ));

                    Image_light.stopAllActions();
                    Image_light.runAction(cc.sequence(
                        cc.delayTime(0.1),
                        cc.fadeOut(0.3)
                    ));

                    Image_op.stopAllActions();
                    Image_op.runAction(cc.sequence(
                        cc.delayTime(0.55),
                        cc.show(),
                        cc.spawn(
                            cc.scaleTo(0.3, 3),
                            cc.fadeOut(0.3)
                        )
                    ));
                }
            }
        }
    },

    getResultFile : function(result){
        var file = "res/ccs/table/effect/peng2.png";
        if (result == 0){
            file = "res/ccs/table/effect/liang.png";
        }else if (result == 2){
            file = "res/ccs/table/effect/peng2.png";
        }else if (result == 4 || result == 5 || result == 6){
            file = "res/ccs/table/effect/gang2.png";
        }else if (result == 3){
            file = "res/ccs/table/effect/hu2.png";
        }else if (result == 33){
            file = "res/ccs/table/effect/zimo.png";
        }
        return file;
    },

    getResultLightFile : function(result){
        var file = "res/ccs/table/effect/peng1.png";
        if (result == 0){
            file = "res/ccs/table/effect/liang1.png";
        }else if (result == 2){
            file = "res/ccs/table/effect/peng1.png";
        }else if (result == 4 || result == 5 || result == 6){
            file = "res/ccs/table/effect/gang1.png";
        }else if (result == 3){
            file = "res/ccs/table/effect/hu2.2.png";
        }else if (result == 33){
            file = "res/ccs/table/effect/zimo2.png";
        }
        return file;
    },

    //0 不显示
    showDealCard : function(cardNums){
        for(var i = 0; i < 3; i++){
            if (cardNums[i] == 0){
                this.Image_dealCard[i].stopAllActions();
                this.Image_dealCard[i].setVisible(false);
            }else{
                this.Image_dealCard[i].stopAllActions();
                this.Image_dealCard[i].setVisible(true);
                this.Image_dealCard[i].runAction(cc.sequence(cc.delayTime(0.6), cc.hide()));

                var scale = 92 / mo.kwxCardHelper.inhandCardSize.width;
                var card = this.Image_dealCard[i].getChildren()[0];
                card.setCardNum(cardNums[i]);
                card.setCardKind(0, 3);
                card.setAnchorPoint(cc.p(0.5, 0.5));
                card.setScale(scale);
                card.setPosition(cc.p(this.Image_dealCard[i].getContentSize().width / 2, this.Image_dealCard[i].getContentSize().height / 2));
            }
        }
    },

    //显示黄庄
    showHuangzhuang : function(){
        this.Image_huangzhuang.stopAllActions();
        this.Image_huangzhuang.setVisible(true);
        this.Image_huangzhuang.setScale(0.5);

        this.Image_huangzhuang.runAction(cc.sequence(
            cc.scaleTo(0.2, 1.2),
            cc.scaleTo(0.1, 1.0),
            cc.delayTime(1.5),
            cc.hide()
        ))
    },

    //显示分数
    setResultScore : function(scores){
        var pos = [
            cc.p(230, 224.5),
            cc.p(mo.gameSize.x - 210, mo.gameSize.y - 225),
            cc.p(230, mo.gameSize.y - 225)
        ];

        //2人场特殊显示
        if (mo.gameMsgManager.kwxGameModel == 2){
            pos = [
                cc.p(230, 224.5),
                cc.p(230, mo.gameSize.y - 225),
                cc.p(230, mo.gameSize.y - 225)
            ];
        }

        var anchorPos = [
            cc.p(0, 0.5),
            cc.p(1, 0.5),
            cc.p(0, 0.5)
        ];

        for (var i = 0; i < scores.length; i++){
            this.AtlasLabel_scores[i].stopAllActions();
            if (scores[i] != 0){
                this.Image_socres[i].setVisible(true);
                this.Image_socres[i].setOpacity(255);
                this.Image_socres[i].setPosition(pos[i]);
                this.Image_socres[i].runAction(cc.sequence(
                    // cc.fadeIn(0.1),
                    cc.delayTime(0.8),
                    cc.spawn(cc.fadeOut(0.2), cc.moveBy(0.2, cc.p(0, 50))),
                    cc.hide()
                ));

                var showStr = "./" + scores[i];
                if (scores[i] > 0){
                    this.AtlasLabel_scores[i].setProperty(showStr, "res/ccs/table/result/piaofen_add.png", 42, 53, ".");
                }else{
                    this.AtlasLabel_scores[i].setProperty(showStr, "res/ccs/table/result/piaofen_sub.png", 42, 53, ".");
                }
            }else{
                this.Image_socres[i].setVisible(false);
            }
        }
    }
});
